#include <iostream>
#include <unistd.h>

#include <libpmemobj++/make_persistent.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/persistent_ptr_base.hpp>
#include <libpmemobj++/pool.hpp>
#include <libpmemobj++/transaction.hpp>


struct root {
    pmem::obj::persistent_ptr<int> pfoo;
};
pmem::obj::persistent_ptr<root> r;

void persistent_ptr_conversion_example(pmem::obj::pool_base &pop) {
    pmem::obj::transaction::run(pop, [&] {
		r->pfoo = pmem::obj::make_persistent<int>(10);
	});
}

void show(pmem::obj::pool_base &pop) {
    std::cout << *r->pfoo << std::endl;
}


static inline int file_exists(char const *file) {
    return access(file, F_OK);
}

int main() {
	pmem::obj::pool<root> pop;
    const char * path = "dst_file";
    if (file_exists(path) != 0) {
        pop = pmem::obj::pool<root>::create(path, "string_example", PMEMOBJ_MIN_POOL);
    } else {
        pop = pmem::obj::pool<root>::open(path, "string_example");
    }
    r = pop.root();
    persistent_ptr_conversion_example(pop);
    show(pop);
	pop.close();
	return 0;
}

